package com.kobe.game_40;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * 
 * Using names.txt (right click and 'Save Link/Target As...'), a 46K text file
 * containing over five-thousand first names, begin by sorting it into
 * alphabetical order. Then working out the alphabetical value for each name,
 * multiply this value by its alphabetical position in the list to obtain a name
 * score.
 * 
 * For example, when the list is sorted into alphabetical order, COLIN, which is
 * worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN
 * would obtain a score of 938 × 53 = 49714.
 * 
 * What is the total of all the name scores in the file?
 * 
 * 
 */

public class Game22 {

    public static void main(String[] args) {

        String[] names = null;
        try {
            names = getNameList();
        } catch (IOException e) {
            e.printStackTrace();
        }

        int result = 0;
        for (int i = 0; i < names.length; i++) {
            result += (i + 1) * getCounts(names[i]);
        }
        System.out.println("result is:  " + result);
    }

    private static String[] getNameList() throws IOException {
        File file = new File("src/source/names.txt");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String names = reader.readLine();
        String[] namesArray = names.split(",");
        return namesArray;
    }

    private static int getCounts(String name) {

        char[] charArray = name.toCharArray();
        int sum = 0;
        for (char tempChar : charArray) {
            switch (tempChar) {
            case 'a':
                sum += 1;
                break;
            case 'b':
                sum += 2;
                break;
            case 'c':
                sum += 3;
                break;
            case 'd':
                sum += 4;
                break;
            case 'e':
                sum += 5;
                break;
            case 'f':
                sum += 6;
                break;
            case 'g':
                sum += 7;
                break;
            case 'h':
                sum += 8;
                break;
            case 'i':
                sum += 9;
                break;
            case 'j':
                sum += 10;
                break;
            case 'k':
                sum += 11;
                break;
            case 'l':
                sum += 12;
                break;
            case 'm':
                sum += 13;
                break;
            case 'n':
                sum += 14;
                break;
            case 'o':
                sum += 15;
                break;
            case 'p':
                sum += 16;
                break;
            case 'q':
                sum += 17;
                break;
            case 'r':
                sum += 18;
                break;
            case 's':
                sum += 19;
                break;
            case 't':
                sum += 20;
                break;
            case 'u':
                sum += 21;
                break;
            case 'v':
                sum += 22;
                break;
            case 'w':
                sum += 23;
                break;
            case 'x':
                sum += 24;
                break;
            case 'y':
                sum += 25;
                break;
            case 'z':
                sum += 26;
                break;
            case 'A':
                sum += 1;
                break;
            case 'B':
                sum += 2;
                break;
            case 'C':
                sum += 3;
                break;
            case 'D':
                sum += 4;
                break;
            case 'E':
                sum += 5;
                break;
            case 'F':
                sum += 6;
                break;
            case 'G':
                sum += 7;
                break;
            case 'H':
                sum += 8;
                break;
            case 'I':
                sum += 9;
                break;
            case 'J':
                sum += 10;
                break;
            case 'K':
                sum += 11;
                break;
            case 'L':
                sum += 12;
                break;
            case 'M':
                sum += 13;
                break;
            case 'N':
                sum += 14;
                break;
            case 'O':
                sum += 15;
                break;
            case 'P':
                sum += 16;
                break;
            case 'Q':
                sum += 17;
                break;
            case 'R':
                sum += 18;
                break;
            case 'S':
                sum += 19;
                break;
            case 'T':
                sum += 20;
                break;
            case 'U':
                sum += 21;
                break;
            case 'V':
                sum += 22;
                break;
            case 'W':
                sum += 23;
                break;
            case 'X':
                sum += 24;
                break;
            case 'Y':
                sum += 25;
                break;
            case 'Z':
                sum += 26;
                break;
            }
        }

        return sum;
    }
}
